//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.accumulategoods;

import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.alarm.alarmclock.AlarmClock;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.nebutown.generation.basicdata.StringMap;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.accumulategoods.support.AbstractAccumulateGoods;
import com.restaurant.application.accumulategoods.support.GoodsData;
import com.restaurant.application.goodscoupon.GoodsCoupon;
import com.restaurant.application.goodscoupon.support.GoodsCouponDetail;
import com.restaurant.application.indexes.accumulategoodslist.AccumulateShop;
import com.restaurant.application.indexes.accumulategoodslist.mysql.AccumulateGoodsListBean;
import com.restaurant.application.indexes.accumulategoodslist.mysql.AccumulateGoodsListImpl;
import com.restaurant.application.level.Level;
import com.restaurant.application.levelset.LevelSet;
import com.restaurant.application.shop.Shop;
import com.restaurant.application.shop.support.ShopData;
import com.restaurant.application.shop.support.ShopVo;
import com.restaurant.application.shoplist.ShopList;
import com.restaurant.application.util.CommonUtils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class AccumulateGoodsImpl extends AbstractAccumulateGoods {
    public AccumulateGoodsImpl() {
    }

    public View getFullView() {
        PageForm pageForm = this.newFormShell(this.getData().duplicate(), DataModes.EDIT, true).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm);
    }

    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    public void upDateState(String state) {
        if (Objects.equals(state, "已下架") || Objects.equals(state, "已上架") || Objects.equals(state, "已删除")) {
            this.getData().setState(state);
        }

        this.quickIndexUpdate();
    }

    public GoodsData getGoodsData() {
        return this.getData().duplicate();
    }

    public String exchangeGoods() {
        if (Objects.isNull(this.getData().getSoldNumber())) {
            this.getData().setSoldNumber(0L);
        }

        if (this.getData().duplicate().getSoldNumber() + 1L > this.getData().getTotalNUmber()) {
            return "商品已售完";
        } else {
            this.getData().setSoldNumber(this.getData().getSoldNumber() + 1L);
            GoodsCouponDetail couponDetail = (new GoodsCouponDetail()).withId(SnowFlake.findUniqueId()).withGoodsUID(this.getId()).withName(this.getData().getGoodsName()).withStartTime(this.getData().getForSellTime()).withId(this.getData().getGoodsId()).withEndTime(this.getData().getRemoveTime()).withNumber(this.getData().getGoodsId() + this.getData().getSoldNumber());
            if (Objects.nonNull(this.getData().getDeliveryType()) && this.getData().getDeliveryType().size() == 1) {
                couponDetail.withType((String)this.getData().getDeliveryType().get(0));
            }

            ((GoodsCoupon)this.createNode(GoodsCoupon.class)).init(couponDetail);
            return "success" + this.getData().duplicate().getGoodsId() + this.getData().duplicate().getSoldNumber();
        }
    }

    public View showGoodsDetail(String uniPosition) {
        PageForm pageForm = this.newFormShell(this.getData().duplicate(), DataModes.SHOW, false).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(uniPosition, pageForm);
    }

    private FormShell newFormShell(GoodsData criteria, DataModes modes, Boolean edit) {
        FormShell formShell = new FormShell("积分商城/发布积分商品", 3, 8);
        formShell.source(criteria, modes);
        formShell.at(0, 0, "商品名称").stringField("goodsName", criteria.getGoodsName()).placeHolder("商品名称").notNull().setEditable(edit);
        formShell.at(0, 1, "门店售价").doubleField("price", criteria.getPrice()).placeHolder("门店售价").setEditable(edit);
        formShell.at(1, 0, "商品描述").stringField("goodsDescribe", criteria.getGoodsDescribe()).placeHolder("商品描述").setEditable(edit);
        formShell.at(2, 0, "图片").styles("", "height:200px").imageentryField("images", criteria.getImages()).notNull().setEditable(edit);
        List<ShopVo> allShopData = ((ShopList)this.getSingleton(ShopList.class)).findAllShopData();
        List<String> shopList = (List)allShopData.stream().map(ShopVo::getName).collect(Collectors.toList());
        formShell.at(2, 1, "使用门店").multipleChoice("useShop", shopList).placeHolder("门店").notNull().setEditable(edit);
        List<UID> allLevel = ((LevelSet)this.getSingleton(LevelSet.class)).findAllLevel();
        List<String> levelList = (List)allLevel.stream().map((e) -> {
            return ((Level)this.getUnit(Level.class, e)).findData().getMemberName();
        }).collect(Collectors.toList());
        formShell.at(3, 0, "兑换级别").multipleChoice("accessLevel", levelList).placeHolder("兑换级别").notNull().setEditable(edit);
        formShell.at(4, 0, "所需积分").longField("accumulatePoints", criteria.getAccumulatePoints()).notNull().placeHolder("所需积分").setEditable(edit);
        formShell.at(4, 1, "总数量").longField("totalNUmber", criteria.getTotalNUmber()).notNull().placeHolder("总数量").setEditable(edit);
        formShell.at(5, 0, "立即上架").checkentryField("immediately", criteria.getImmediately()).placeHolder("立即上架").setEditable(edit);
        formShell.at(6, 0, "上架时间").dateEntryField("forSellTime", criteria.getForSellTime(), DateTypes.DATETIME).placeHolder("上架时间").setEditable(edit);
        formShell.at(7, 0, "下架时间").dateEntryField("removeTime", criteria.getRemoveTime(), DateTypes.DATETIME).placeHolder("下架时间").setEditable(edit);
        List<String> deliveryType = Lists.newArrayList(new String[]{"到店自提", "物流配送"});
        formShell.at(3, 0, "配送方式").multipleChoice("deliveryType", deliveryType).placeHolder("配送方式").notNull().setEditable(edit);
        if (edit) {
            formShell.at(8, 0, "").buttonField(this.getPath(CMD_SAVE, new int[0]), "保存", "创建", true);
        }

        formShell.at(8, 1, "").buttonField(this.getPath(CMD_CANCEL, new int[0]), "取消", "取消", true);
        return formShell;
    }

    public ViewUpdate save(GoodsData save, int[] seq) {
        FormShell formShell = this.newFormShell(save, DataModes.VALIDATE, true);
        boolean b = formShell.dataFailedValidation();
        if (b) {
            PageForm pageForm = this.newFormShell(save, DataModes.VALIDATE, true).getForm();
            return this.returnView(new View[]{((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm)});
        } else {
            save.withModifyTime(DateTimeUtils.getCurrentMillisecond());
            if (Objects.nonNull(save.getAccessLevel()) && save.getAccessLevel().size() > 0) {
                save.getAccessLevel().forEach((x) -> {
                    List<UID> collect = (List)((LevelSet)this.getSingleton(LevelSet.class)).findAllLevel().stream().filter((e) -> {
                        return Objects.equals(((Level)this.getUnit(Level.class, e)).findData().getMemberName(), x);
                    }).collect(Collectors.toList());
                    if (collect.size() > 0) {
                        save.getLevelUid().add((UID)collect.get(0));
                    }

                });
            }

            save.withGoodsCategory("其他");
            if (Objects.nonNull(this.getData().getSoldNumber()) && this.getData().getSoldNumber() > save.getTotalNUmber()) {
                return this.returnViewCall(this.buildViewCall(this.unitPosition(), "showErrorMessage", (List)null, (new StringMap()).putData("errorMessage", "商品总数量不能小于已卖出的数量")));
            } else {
                if (Objects.nonNull(save.getUseShop())) {
                    save.getUseShop().forEach((e) -> {
                        UID shopByName = ((ShopList)this.getSingleton(ShopList.class)).findShopByName(e);
                        ShopData data = ((Shop)this.getUnit(Shop.class, shopByName)).findData();
                        save.getUseShopId().add(data.getId());
                        save.getUseShopUid().add(shopByName);
                    });
                }

                save.withCreateTime(this.getData().getCreateTime());
                save.withGoodsId(this.getData().getGoodsId());
                if (Objects.nonNull(this.getData().getSoldNumber())) {
                    save.withSoldNumber(this.getData().getSoldNumber());
                }

                this.setData(save);
                if (Objects.nonNull(save.getImmediately()) && save.getImmediately()) {
                    this.getData().setState("已上架");
                    this.getData().setForSellTime(DateTimeUtils.getCurrentMillisecond());
                }

                if (Objects.nonNull(this.getData().getRemoveTime())) {
                    ((AlarmClock)this.getSingleton(AlarmClock.class)).setDelayAlarmClock(this.getId().toString() + "remove", (this.getData().getRemoveTime() - DateTimeUtils.getCurrentMillisecond()) / 1000L, this.getId());
                }

                if (Objects.nonNull(this.getData().getForSellTime())) {
                    ((AlarmClock)this.getSingleton(AlarmClock.class)).setDelayAlarmClock(this.getId().toString() + "forSell", (this.getData().getForSellTime() - DateTimeUtils.getCurrentMillisecond()) / 1000L, this.getId());
                }

                if (Objects.isNull(this.getData().getCreateTime())) {
                    this.getData().setCreateTime(DateTimeUtils.getCurrentMillisecond());
                    this.getData().setGoodsId(CommonUtils.getGoodsNumber(this));
                    this.insertGoods();
                }

                this.quickIndexUpdate();
                return this.returnPushInContainer(((AccumulateShop)this.getSingleton(AccumulateShop.class)).getId(), false);
            }
        }
    }

    private void insertGoods() {
        AccumulateGoodsListBean bean = new AccumulateGoodsListBean();
        this.getGoodsUIDDataForAccumulateGoodsList(bean);
        ((AccumulateGoodsListImpl)this.getIndex(AccumulateGoodsListImpl.class)).insert(bean);
    }

    public ViewUpdate cancel(int[] seq) {
        return this.returnPushInContainer(((AccumulateShop)this.getSingleton(AccumulateShop.class)).getId(), false);
    }

    public boolean timerCall(String name) {
        System.out.println("我来了-------" + DateTimeUtils.getCurrentDateTimeStr());
        if (Objects.equals(name, this.getId().toString() + "remove")) {
            if (this.getData().getRemoveTime() <= DateTimeUtils.getCurrentMillisecond()) {
                this.getData().setState("已下架");
                return false;
            }
        } else if (Objects.equals(name, this.getId().toString() + "forSell") && this.getData().getForSellTime() <= DateTimeUtils.getCurrentMillisecond()) {
            this.getData().setState("已上架");
            return false;
        }

        return true;
    }

    public AccumulateGoodsListBean getGoodsUIDDataForAccumulateGoodsList(AccumulateGoodsListBean bean) {
        GoodsData data = this.getData().duplicate();
        bean.withAccumulatePoints(data.getAccumulatePoints()).withCreateTime(data.getCreateTime()).withModifyTime(data.getModifyTime()).withGoodsNumber(data.getTotalNUmber()).withGoodsDescribe(data.getGoodsDescribe()).withGoodsId(data.getGoodsId()).withGoodsName(data.getGoodsName()).withGoodsUID(this.getId()).withSoldNumber(this.getData().getSoldNumber()).withGoodsType(data.getGoodsCategory()).withTypeId(data.getTypeId()).withPrice(this.getData().getPrice()).withState(this.getData().getState()).withUpTime(this.getData().getForSellTime()).withDownTime(this.getData().getRemoveTime()).withExchangeLevel(Objects.nonNull(this.getData().getAccessLevel()) ? this.getData().getAccessLevel().toString() : "");
        if (Objects.nonNull(this.getData().getUseShop()) && this.getData().getUseShop().size() > 0) {
            bean.withShopName(this.getData().getUseShop().toString().substring(1, this.getData().getUseShop().toString().length() - 1));
            bean.withShopId(this.getData().getUseShopId().toString().substring(1, this.getData().getUseShopId().toString().length() - 1));
        }

        return bean;
    }
}
